package com.sanqi.weixin.controller;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sanqi.common.annotation.Log;
import com.sanqi.common.core.controller.BaseController;
import com.sanqi.common.core.domain.AjaxResult;
import com.sanqi.common.enums.BusinessType;
import com.sanqi.weixin.domain.WxFile;
import com.sanqi.weixin.service.IWxFileService;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.sanqi.common.utils.poi.ExcelUtil;
import com.sanqi.common.core.page.TableDataInfo;

/**
 * 导入文件Controller
 * 
 * @author duany
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/weixin/file")
@Api(tags="文件导入")
public class WxFileController extends BaseController
{
    @Autowired
    private IWxFileService wxFileService;

    /**
     * 查询导入文件列表
     */
    @PreAuthorize("@ss.hasPermi('weixin:file:list')")
    @GetMapping("/list")
    @ApiOperation(value="文件导入-列表", notes="文件导入-列表")
    public TableDataInfo list(WxFile wxFile)
    {
        startPage();
        List<WxFile> list = wxFileService.selectWxFileList(wxFile);
        return getDataTable(list);
    }

    /**
     * 导出导入文件列表
     */
    @PreAuthorize("@ss.hasPermi('weixin:file:export')")
    @Log(title = "导入文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxFile wxFile)
    {
        List<WxFile> list = wxFileService.selectWxFileList(wxFile);
        ExcelUtil<WxFile> util = new ExcelUtil<WxFile>(WxFile.class);
        util.exportExcel(response, list, "导入文件数据");
    }

    /**
     * 获取导入文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('weixin:file:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value="文件导入-获取导入文件详细信息", notes="文件导入-获取导入文件详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(wxFileService.selectWxFileById(id));
    }

    /**
     * 新增导入文件
     */
    @PreAuthorize("@ss.hasPermi('weixin:file:add')")
    @Log(title = "导入文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxFile wxFile)
    {
        return toAjax(wxFileService.insertWxFile(wxFile));
    }

    /**
     * 修改导入文件
     */
    @PreAuthorize("@ss.hasPermi('weixin:file:edit')")
    @Log(title = "导入文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxFile wxFile)
    {
        return toAjax(wxFileService.updateWxFile(wxFile));
    }

    /**
     * 删除导入文件
     */
    @PreAuthorize("@ss.hasPermi('weixin:file:remove')")
    @Log(title = "导入文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value="文件导入-删除导入文件", notes="文件导入-删除导入文件")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        List<WxFile> files = this.wxFileService.listByIds(CollUtil.newArrayList(ids));
        int num = wxFileService.deleteWxFileByIds(ids);
        if(0 != num) {
            for (WxFile wxFile : files) {
                String filePath = wxFile.getFilePath();
                String newFilePaths = wxFile.getNewFilePaths();
                FileUtil.del(newFilePaths);
                FileUtil.del(filePath);
            }
        }
        return toAjax(num);
    }
    
    /**
     * @Description 导入文件并进行计算
     * @param file
     * @param request
     * @return
     * Create at: 2025-10-13 10:30:51
     * @author: duany
     * Revision:
     *    2025-10-13 10:30:51 - first revision by duany
     *        
     */
    @PostMapping("/impFile")
    @ApiOperation(value="文件导入-导入文件", notes="文件导入-导入文件")
    @Log(title = "导入文件", businessType = BusinessType.DELETE)
    
    public AjaxResult impFile(MultipartFile file, HttpServletRequest request) {
        String taskNo = request.getParameter("taskNo");
        String workplaceId = request.getParameter("workplaceId");
        WxFile wxFile = this.wxFileService.saveFile(file, taskNo, workplaceId);
        wxFileService.dealwithFile(wxFile.getId());
        return StrUtil.isBlankIfStr(wxFile.getId()) ? success(this.wxFileService.getById(wxFile.getId()))
                : error(wxFile.getSendException());
    }
}
